//
//  NavigationController.swift
//  cxd4iphone
//
//  Created by hexy on 15/11/19.
//  Copyright © 2015年 hexy. All rights reserved.
//

import UIKit

class NavigationController: UINavigationController {
    
//    init() {
//        super.init(navigationBarClass: NavigationBar.self, toolbarClass: nil)
//    }
//    
//    override init(rootViewController: UIViewController) {
//
//        super.init(navigationBarClass: NavigationBar.self, toolbarClass: nil)
//        self.viewControllers = [rootViewController]
//    }
//    
//    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
//        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
//    }
//
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.interactivePopGestureRecognizer?.delegate = self
    }

    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        if childViewControllers.count > 0 {
            
            let rootController = childViewControllers[0]
            
            if viewController != rootController {
                
                let leftItem = UIBarButtonItem(title: " 返回", image: UIImage(named: "back")!, target: self, action: "back")
                viewController.navigationItem.leftBarButtonItem = leftItem
            
                viewController.hidesBottomBarWhenPushed = true
            }
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        
        return UIStatusBarStyle.LightContent
    }
    
    func back() {
        self.popViewControllerAnimated(true)
    }
}

extension NavigationController: UIGestureRecognizerDelegate {
    
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        return self.childViewControllers.count > 1
    }

}
